package com.y.ari.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.y.ari.common.AriException;
import com.y.ari.common.RestResponse;
import com.y.ari.dto.CabinsDTO;
import com.y.ari.dto.FlightDTO;
import com.y.ari.dto.FlightFormDTO;
import com.y.ari.dto.PageDTO;
import com.y.ari.entity.Cabins;
import com.y.ari.entity.Flight;
import com.y.ari.entity.PageParams;
import com.y.ari.mapper.CabinsMapper;
import com.y.ari.mapper.FlightMapper;
import com.y.ari.service.FlightService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 航班信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class FlightServiceImpl extends ServiceImpl<FlightMapper, Flight> implements FlightService {

    @Autowired
    FlightMapper flightMapper;

    @Autowired
    CabinsMapper cabinsMapper;

    @Transactional
    @Override
    public List<FlightDTO> selFlight(FlightFormDTO flightFormDTO) {

        List<FlightDTO> flightDTOS = flightMapper.selFlight(flightFormDTO);
        if (flightDTOS.isEmpty()) {
            return null;
        }
        return flightDTOS;
    }


    @Transactional
    @Override
    public PageDTO selPage(PageParams pageParams) {
        Integer offset = Math.toIntExact((pageParams.getPageNo() - 1) * pageParams.getPageSize());
        Integer pageSize = Math.toIntExact(pageParams.getPageSize());

        List<FlightDTO> flightDTOS = flightMapper.selPage();

        List<FlightDTO> newList = flightDTOS.subList(offset, Math.min(flightDTOS.size() - offset, pageSize) + offset);

        LambdaQueryWrapper<Flight> queryWrapper = new LambdaQueryWrapper<>();
        Integer count = flightMapper.selectCount(queryWrapper);

        PageDTO pageDTO = new PageDTO(newList, count, pageParams.getPageNo(), pageParams.getPageSize());
        return pageDTO;
    }

    @Transactional
    @Override
    public RestResponse saveFC(FlightDTO flightDTO) {
        Flight flight = new Flight();
        if (flightDTO.getId() == null) {
            BeanUtils.copyProperties(flightDTO, flight);
            int iFli = flightMapper.insert(flight);
            if (iFli == 0) {
                AriException.cast("添加失败");
            }

            List<CabinsDTO> cabins = flightDTO.getCabins();

            if (cabins.isEmpty()) {
                return RestResponse.success("添加成功");
            }

            for (CabinsDTO c : cabins) {
                c.setFlightId(flight.getId());
            }
            int ic = cabinsMapper.batchInsertC(cabins);
            if (ic == 0) {
                AriException.cast("添加失败");
            }
            return RestResponse.success("添加成功");
        }

        int iFli = flightMapper.updateById(flight);
        if (iFli == 0) {
            AriException.cast("修改失败");
        }

        //修改舱位
        LambdaQueryWrapper<Cabins> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cabins::getFlightId, flightDTO.getId());
        cabinsMapper.delete(queryWrapper);

        List<CabinsDTO> cabins = flightDTO.getCabins();
        if (cabins.isEmpty()) {
            return RestResponse.success("修改成功");
        }
        for (CabinsDTO c : cabins) {
            c.setFlightId(flightDTO.getId());
        }
        int ic = cabinsMapper.batchInsertC(cabins);
        if (ic == 0) {
            AriException.cast("添加失败");
        }
        return RestResponse.success("修改成功");
    }

    @Transactional
    @Override
    public RestResponse delFC(Long id) {
        int iFl = flightMapper.deleteById(id);
        LambdaQueryWrapper<Cabins> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cabins::getFlightId, id);
        int iC = cabinsMapper.delete(queryWrapper);
        if (iC == 0 && iFl == 0) {
            AriException.cast("删除失败");
        }
        return RestResponse.success("删除成功");
    }
}
